package Java学习.集合;

import Java学习.基础.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class 遍历集合 {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        //键盘录入学生信息,并添加到集合中
        for (int i = 0; i < 3; i++) {
            System.out.print("请输入您要录入学生的姓名:");
            String name=sc.next();
            System.out.print("请输入您要添加的学生的年龄:");
            int age=sc.nextInt();
            Student s=new Student(name,age);
            list.add(s);
        }
        //遍历集合
        System.out.print("[");
        for (int i = 0; i < list.size(); i++) {
            if (i< list.size()-1){
                System.out.print(list.get(i).getName()+",");
            }else {
                System.out.print(list.get(i).getName()+"]");
            }
        }
    }
}
